package com.bailiban.aigou.admin.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.bailiban.aigou.admin.dao.UserAdminDao;
import com.bailiban.aigou.admin.entity.BackstageUser;
import com.bailiban.aigou.admin.utils.PageModel;

@Repository
public class UserAdminDaoImpl implements UserAdminDao{
	@Resource
	private SessionFactory sessionFactory;
	/**
	 * 
	 * 分页显示
	 * **/
	public PageModel<BackstageUser> getAdminUser(int pageNo, int pageSize) {
		PageModel<BackstageUser> pageModel = new PageModel<BackstageUser>();
		pageModel.setPageNo(pageNo);
		pageModel.setPageSize(pageSize);
		
		Session session = sessionFactory.getCurrentSession();
		
		Criteria criteria = session.createCriteria(BackstageUser.class);
		criteria.setFirstResult((pageNo-1)*pageSize);
		criteria.setMaxResults(pageSize);
		
		List<BackstageUser> list = criteria.list();
		pageModel.setList(list);
		return pageModel;
	}

	public int getAdminUserCount() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count(id) from BackstageUser");
		List<Long> list = query.list();
		return list.get(0).intValue();
	}
	/**
	 * 添加用户
	 */
	public void save(BackstageUser user) {
		sessionFactory.getCurrentSession().save(user);
		
	}
	/**
	 * id查用户
	 */
	public BackstageUser getIdByUser(String id) {
		Session session = sessionFactory.getCurrentSession();
		BackstageUser user = session.get(BackstageUser.class, id);
		return user;
	}
    /**
     * 修改用户
     * */
	public void updateUser(BackstageUser u) {
		Session session = sessionFactory.getCurrentSession();
		session.update(u);
		
	}
     /**
      * 删除用户
      * */
	public void deleteuser(String id) {
		Session session = sessionFactory.getCurrentSession();
		BackstageUser user = getIdByUser(id);
		session.delete(user);
		
	}
	/**
	 * 根据nam 查询对象
	 * @param name
	 * @return
	 */
    @Override
	public BackstageUser getNameUser(String username) {
		Session session = sessionFactory.getCurrentSession();
		BackstageUser user = session.get(BackstageUser.class, username);
		return user;
	}

	
	
}
